#include "volumetool.h"
#include "ui_volumetool.h"
#include <QGraphicsDropShadowEffect>
#include <QPainter>
#include <QDebug>

VolumeTool::VolumeTool(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::VolumeTool),
    isMted(false),
    volumeRatio(20)
{
    ui->setupUi(this);
    // 设置弹出窗口，无边框，去掉默认阴影
    setWindowFlags(Qt::Popup | Qt::FramelessWindowHint| Qt::NoDropShadowWindowHint);
    // 透明
    setAttribute(Qt::WA_TranslucentBackground);
    // 自定义阴影
    QGraphicsDropShadowEffect* shadow = new QGraphicsDropShadowEffect(this);
    shadow->setOffset(0, 0);
    shadow->setColor("#646464");
    shadow->setBlurRadius(10);
    this->setGraphicsEffect(shadow);
    // 给按钮设置图标
    ui->silenceBtn->setIcon(QIcon(":/images/volumn.png"));
    // 设置初始音量显示20%
    ui->volumeRatio->setText("20%");
    // 音量条高度36，y+36
    QRect rect = ui->utSlider->geometry();
    ui->utSlider->setGeometry(rect.x(), 180 + 25 - 36,
                              rect.width(), 36);
    // 移动按钮位置，音量的y减去按钮的半径
    ui->sliderBtn->move(ui->sliderBtn->x(), ui->utSlider->y() - ui->sliderBtn->height() / 2);
    // 安装事件过滤器
    ui->sliderBox->installEventFilter(this);
}

VolumeTool::~VolumeTool()
{
    delete ui;
}

void VolumeTool::paintEvent(QPaintEvent *event)
{
    (void)event;
    QPainter painter(this);
    // 设置抗锯齿
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setPen(Qt::NoPen);
    painter.setBrush(Qt::white);
    // 三角形
    QPolygon polygon;
    polygon.append(QPoint(30, 300));
    polygon.append(QPoint(70, 300));
    polygon.append(QPoint(50, 320));

    painter.drawPolygon(polygon);
}

bool VolumeTool::eventFilter(QObject *object, QEvent *event)
{
    // 事件控件
    if (object == ui->sliderBox)
    {
        if (event->type() == QEvent::MouseButtonPress)
        {
            setVolume();
            emit setMusicVolume(volumeRatio);
        }
        else if(event->type() == QEvent::MouseMove)
        {
            setVolume();
            emit setMusicVolume(volumeRatio);
        }
        else if (event->type() == QEvent::MouseButtonRelease)
        {
            emit setMusicVolume(volumeRatio);
        }
    }

    return QObject::eventFilter(object, event);
}

void VolumeTool::setVolume()
{
    // 获取鼠标相对于控件的坐标
    int height = ui->sliderBox->mapFromGlobal(QCursor().pos()).y();
    // 是否超过范围 【25,205】
    height = height < 25? 25 : height;
    height = height > 205? 205 : height;
    // 调整按钮位置
    ui->sliderBtn->move(ui->sliderBtn->x(), height - ui->sliderBtn->height() / 2);
    // 更新outline的位置
    ui->utSlider->setGeometry(ui->utSlider->x(), height, ui->utSlider->width(), 205 - height);
    // 计算音量比率
    volumeRatio = (int)(ui->utSlider->height() / (float)180 * 100);
    // 设置显示
    ui->volumeRatio->setText(QString::number(volumeRatio) + "%");
}

void VolumeTool::on_silenceBtn_clicked()
{
    isMted = !isMted;
    if (isMted)
    {
        ui->silenceBtn->setIcon(QIcon(":/images/silent.png"));
    }
    else
    {
        ui->silenceBtn->setIcon(QIcon(":/images/volumn.png"));
    }
    emit setSilence(isMted);
}
